#!/bin/sh

# Slackware build script for openproj

# Written by Chris Abela <chris.abela@maltats.com>
# May 2009

PRGNAM=openproj
VERSION=${VERSION:-1.4}
ARCH=noarch		# Leave this alone
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

OPENPROJ_BUILD=$TMP/$PRGNAM-$VERSION-src/openproj_build

set -e	# Exit on most errors

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION-src
tar xvf $CWD/$PRGNAM-$VERSION-src.tar.gz

cd $PRGNAM-$VERSION-src
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# The following is adopted from $PRGNAM-$VERSION-src/open_build/make_deb.sh
cd openproj_contrib
ant build-contrib build-script build-exchange build-reports
JAVA_OPTS="-Xmx128m"
java $JAVA_OPTS -jar ant-lib/proguard.jar @openproj_contrib.conf
java $JAVA_OPTS -jar ant-lib/proguard.jar @openproj_script.conf
java $JAVA_OPTS -jar ant-lib/proguard.jar @openproj_exchange.conf
java $JAVA_OPTS -jar ant-lib/proguard.jar @openproj_exchange2.conf
java $JAVA_OPTS -jar ant-lib/proguard.jar @openproj_reports.conf
cd ../openproj_build
ant -Dbuild_contrib=false

cd $PKG
mkdir -p usr/{bin,doc/$PRGNAM-$VERSION,share/{$PRGNAM,applications,pixmaps,mime/packages}}
install -m 755 $OPENPROJ_BUILD/resources/openproj.sh usr/bin/openproj
patch -d usr/bin -i $CWD/openproj.diff
cp $OPENPROJ_BUILD/resources/openproj.desktop usr/share/applications/
cp $OPENPROJ_BUILD/resources/openproj.png usr/share/pixmaps/
cp $OPENPROJ_BUILD/resources/openproj.xml usr/share/mime/packages/
cp -r $OPENPROJ_BUILD/{license,resources/samples} usr/doc/$PRGNAM-$VERSION/
cp -r $OPENPROJ_BUILD/dist/* usr/share/$PRGNAM/

chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 700 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;


cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
